home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Video / getpic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-23  |  33.6 KB  |  1,241 lines

  1. /* getpic.c, picture decoding                                               */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #include "config.h"
  33. #include "global.h"
  34.  
  35. /* private prototypes*/
  36. static void picture_data _ANSI_ARGS_((int framenum));
  37. static void macroblock_modes _ANSI_ARGS_((int *pmacroblock_type, int *pstwtype,
  38.   int *pstwclass, int *pmotion_type, int *pmotion_vector_count, int *pmv_format, int *pdmv,
  39.   int *pmvscale, int *pdct_type));
  40. static void Clear_Block _ANSI_ARGS_((int comp));
  41. static void Sum_Block _ANSI_ARGS_((int comp));
  42. static void Saturate _ANSI_ARGS_((short *bp));
  43. static void Add_Block _ANSI_ARGS_((int comp, int bx, int by,
  44.   int dct_type, int addflag));
  45. static void Update_Picture_Buffers _ANSI_ARGS_((void));
  46. static void frame_reorder _ANSI_ARGS_((int bitstream_framenum, 
  47.   int sequence_framenum));
  48. static void Decode_SNR_Macroblock _ANSI_ARGS_((int *SNRMBA, int *SNRMBAinc, 
  49.   int MBA, int MBAmax, int *dct_type));
  50.  
  51. static void motion_compensation _ANSI_ARGS_((int MBA, int macroblock_type, 
  52.  int motion_type, int PMV[2][2][2], int motion_vertical_field_select[2][2], 
  53.  int dmvector[2], int stwtype, int dct_type));
  54.  
  55. static void skipped_macroblock _ANSI_ARGS_((int dc_dct_pred[3], 
  56.   int PMV[2][2][2], int *motion_type, int motion_vertical_field_select[2][2],
  57.   int *stwtype, int *macroblock_type));
  58.  
  59. static int slice _ANSI_ARGS_((int framenum, int MBAmax));
  60.  
  61. static int start_of_slice _ANSI_ARGS_ ((int MBAmax, int *MBA,
  62.   int *MBAinc, int dc_dct_pred[3], int PMV[2][2][2]));
  63.  
  64. static int decode_macroblock _ANSI_ARGS_((int *macroblock_type, 
  65.   int *stwtype, int *stwclass, int *motion_type, int *dct_type,
  66.   int PMV[2][2][2], int dc_dct_pred[3], 
  67.   int motion_vertical_field_select[2][2], int dmvector[2]));
  68.  
  69.  
  70. /* decode one frame or field picture */
  71. void Decode_Picture(bitstream_framenum, sequence_framenum)
  72. int bitstream_framenum, sequence_framenum;
  73. {
  74.  
  75.   if (picture_structure==FRAME_PICTURE && Second_Field)
  76.   {
  77.     /* recover from illegal number of field pictures */
  78.     printf("odd number of field pictures\n");
  79.     Second_Field = 0;
  80.   }
  81.  
  82.   /* IMPLEMENTATION: update picture buffer pointers */
  83.   Update_Picture_Buffers();
  84.  
  85. #ifdef VERIFY 
  86.   Check_Headers(bitstream_framenum, sequence_framenum);
  87. #endif /* VERIFY */
  88.  
  89.   /* ISO/IEC 13818-4 section 2.4.5.4 "frame buffer intercept method" */
  90.   /* (section number based on November 1995 (Dallas) draft of the 
  91.       conformance document) */
  92.   if(Ersatz_Flag)
  93.     Substitute_Frame_Buffer(bitstream_framenum, sequence_framenum);
  94.  
  95.   /* form spatial scalable picture */
  96.  
  97.   /* form spatial scalable picture */
  98.   /* ISO/IEC 13818-2 section 7.7: Spatial scalability */
  99.   if (base.pict_scal && !Second_Field) 
  100.   {
  101.     Spatial_Prediction();
  102.   }
  103.  
  104.   /* decode picture data ISO/IEC 13818-2 section 6.2.3.7 */
  105.   picture_data(bitstream_framenum);
  106.  
  107.   /* write or display current or previously decoded reference frame */
  108.   /* ISO/IEC 13818-2 section 6.1.1.11: Frame reordering */
  109.   frame_reorder(bitstream_framenum, sequence_framenum);
  110.  
  111.   if (picture_structure!=FRAME_PICTURE)
  112.     Second_Field = !Second_Field;
  113. }
  114.  
  115.  
  116. /* decode all macroblocks of the current picture */
  117. /* stages described in ISO/IEC 13818-2 section 7 */
  118. static void picture_data(framenum)
  119. int framenum;
  120. {
  121.   int MBAmax;
  122.   int ret;
  123.  
  124.   /* number of macroblocks per picture */
  125.   MBAmax = mb_width*mb_height;
  126.  
  127.   if (picture_structure!=FRAME_PICTURE)
  128.     MBAmax>>=1; /* field picture has half as mnay macroblocks as frame */
  129.  
  130.   for(;;)
  131.   {
  132.     if((ret=slice(framenum, MBAmax))<0)
  133.       return;
  134.   }
  135.  
  136. }
  137.  
  138.  
  139.  
  140. /* decode all macroblocks of the current picture */
  141. /* ISO/IEC 13818-2 section 6.3.16 */
  142. static int slice(framenum, MBAmax)
  143. int framenum, MBAmax;
  144. {
  145.   int MBA; 
  146.   int MBAinc, macroblock_type, motion_type, dct_type;
  147.   int dc_dct_pred[3];
  148.   int PMV[2][2][2], motion_vertical_field_select[2][2];
  149.   int dmvector[2];
  150.   int stwtype, stwclass;
  151.   int SNRMBA, SNRMBAinc;
  152.   int ret;
  153.  
  154.   MBA = 0; /* macroblock address */
  155.   MBAinc = 0;
  156.  
  157.   if((ret=start_of_slice(MBAmax, &MBA, &MBAinc, dc_dct_pred, PMV))!=1)
  158.     return(ret);
  159.  
  160.   if (Two_Streams && enhan.scalable_mode==SC_SNR)
  161.   {
  162.     SNRMBA=0;
  163.     SNRMBAinc=0;
  164.   }
  165.  
  166.   Fault_Flag=0;
  167.  
  168.   for (;;)
  169.   {
  170.  
  171.     /* this is how we properly exit out of picture */
  172.     if (MBA>=MBAmax)
  173.       return(-1); /* all macroblocks decoded */
  174.  
  175. #ifdef TRACE
  176.     if (Trace_Flag)
  177.       printf("frame %d, MB %d\n",framenum,MBA);
  178. #endif /* TRACE */
  179.  
  180. #ifdef DISPLAY
  181.     if (!progressive_frame && picture_structure==FRAME_PICTURE 
  182.       && MBA==(MBAmax>>1) && framenum!=0 && Output_Type==T_X11 
  183.        && !Display_Progressive_Flag)
  184.     {
  185.       Display_Second_Field();
  186.     }
  187. #endif
  188.  
  189.     ld = &base;
  190.  
  191.     if (MBAinc==0)
  192.     {
  193.       if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
  194.           ld = &enhan;
  195.  
  196.       if (!Show_Bits(23) || Fault_Flag) /* next_start_code or fault */
  197.       {
  198. resync: /* if Fault_Flag: resynchronize to next next_start_code */
  199.         Fault_Flag = 0;
  200.         return(0);     /* trigger: go to next slice */
  201.       }
  202.       else /* neither next_start_code nor Fault_Flag */
  203.       {
  204.         if (base.scalable_mode==SC_DP && base.priority_breakpoint==1)
  205.           ld = &enhan;
  206.  
  207.         /* decode macroblock address increment */
  208.         MBAinc = Get_macroblock_address_increment();
  209.  
  210.         if (Fault_Flag) goto resync;
  211.       }
  212.     }
  213.  
  214.     if (MBA>=MBAmax)
  215.     {
  216.       /* MBAinc points beyond picture dimensions */
  217.       if (!Quiet_Flag)
  218.         printf("Too many macroblocks in picture\n");
  219.       return(-1);
  220.     }
  221.  
  222.     if (MBAinc==1) /* not skipped */
  223.     {
  224.       ret = decode_macroblock(¯oblock_type, &stwtype, &stwclass,
  225.               &motion_type, &dct_type, PMV, dc_dct_pred, 
  226.               motion_vertical_field_select, dmvector);
  227.  
  228.       if(ret==-1)
  229.         return(-1);
  230.    
  231.       if(ret==0)
  232.         goto resync;
  233.  
  234.     }
  235.     else /* MBAinc!=1: skipped macroblock */
  236.     {      
  237.       /* ISO/IEC 13818-2 section 7.6.6 */
  238.       skipped_macroblock(dc_dct_pred, PMV, &motion_type, 
  239.         motion_vertical_field_select, &stwtype, ¯oblock_type);
  240.     }
  241.  
  242.     /* SCALABILITY: SNR */
  243.     /* ISO/IEC 13818-2 section 7.8 */
  244.     /* NOTE: we currently ignore faults encountered in this routine */
  245.     if (Two_Streams && enhan.scalable_mode==SC_SNR)
  246.       Decode_SNR_Macroblock(&SNRMBA, &SNRMBAinc, MBA, MBAmax, &dct_type);
  247.  
  248.     /* ISO/IEC 13818-2 section 7.6 */
  249.     motion_compensation(MBA, macroblock_type, motion_type, PMV, 
  250.       motion_vertical_field_select, dmvector, stwtype, dct_type);
  251.  
  252.  
  253.     /* advance to next macroblock */
  254.     MBA++;
  255.     MBAinc--;
  256.  
  257.     /* SCALABILITY: SNR */
  258.     if (Two_Streams && enhan.scalable_mode==SC_SNR)
  259.     {
  260.       SNRMBA++;
  261.       SNRMBAinc--;
  262.     }
  263.  
  264.     if (MBA>=MBAmax)
  265.       return(-1); /* all macroblocks decoded */
  266.   }
  267. }
  268.  
  269.  
  270. /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
  271. static void macroblock_modes(pmacroblock_type,pstwtype,pstwclass,
  272.   pmotion_type,pmotion_vector_count,pmv_format,pdmv,pmvscale,pdct_type)
  273.   int *pmacroblock_type, *pstwtype, *pstwclass;
  274.   int *pmotion_type, *pmotion_vector_count, *pmv_format, *pdmv, *pmvscale;
  275.   int *pdct_type;
  276. {
  277.   int macroblock_type;
  278.   int stwtype, stwcode, stwclass;
  279.   int motion_type = 0;
  280.   int motion_vector_count, mv_format, dmv, mvscale;
  281.   int dct_type;
  282.   static unsigned char stwc_table[3][4]
  283.     = { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
  284.   static unsigned char stwclass_table[9]
  285.     = {0, 1, 2, 1, 1, 2, 3, 3, 4};
  286.  
  287.   /* get macroblock_type */
  288.   macroblock_type = Get_macroblock_type();
  289.  
  290.   if (Fault_Flag) return;
  291.  
  292.   /* get spatial_temporal_weight_code */
  293.   if (macroblock_type & MB_WEIGHT)
  294.   {
  295.     if (spatial_temporal_weight_code_table_index==0)
  296.       stwtype = 4;
  297.     else
  298.     {
  299.       stwcode = Get_Bits(2);
  300. #ifdef TRACE
  301.       if (Trace_Flag)
  302.       {
  303.         printf("spatial_temporal_weight_code (");
  304.         Print_Bits(stwcode,2,2);
  305.         printf("): %d\n",stwcode);
  306.       }
  307. #endif /* TRACE */
  308.       stwtype = stwc_table[spatial_temporal_weight_code_table_index-1][stwcode];
  309.     }
  310.   }
  311.   else
  312.     stwtype = (macroblock_type & MB_CLASS4) ? 8 : 0;
  313.  
  314.   /* SCALABILITY: derive spatial_temporal_weight_class (Table 7-18) */
  315.   stwclass = stwclass_table[stwtype];
  316.  
  317.   /* get frame/field motion type */
  318.   if (macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD))
  319.   {
  320.     if (picture_structure==FRAME_PICTURE) /* frame_motion_type */
  321.     {
  322.       motion_type = frame_pred_frame_dct ? MC_FRAME : Get_Bits(2);
  323. #ifdef TRACE
  324.       if (!frame_pred_frame_dct && Trace_Flag)
  325.       {
  326.         printf("frame_motion_type (");
  327.         Print_Bits(motion_type,2,2);
  328.         printf("): %s\n",motion_type==MC_FIELD?"Field":
  329.                          motion_type==MC_FRAME?"Frame":
  330.                          motion_type==MC_DMV?"Dual_Prime":"Invalid");
  331.       }
  332. #endif /* TRACE */
  333.     }
  334.     else /* field_motion_type */
  335.     {
  336.       motion_type = Get_Bits(2);
  337. #ifdef TRACE
  338.       if (Trace_Flag)
  339.       {
  340.         printf("field_motion_type (");
  341.         Print_Bits(motion_type,2,2);
  342.         printf("): %s\n",motion_type==MC_FIELD?"Field":
  343.                          motion_type==MC_16X8?"16x8 MC":
  344.                          motion_type==MC_DMV?"Dual_Prime":"Invalid");
  345.       }
  346. #endif /* TRACE */
  347.     }
  348.   }
  349.   else if ((macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
  350.   {
  351.     /* concealment motion vectors */
  352.     motion_type = (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD;
  353.   }
  354. #if 0
  355.   else
  356.   {
  357.     printf("maroblock_modes(): unknown macroblock type\n");
  358.     motion_type = -1;
  359.   }
  360. #endif
  361.  
  362.   /* derive motion_vector_count, mv_format and dmv, (table 6-17, 6-18) */
  363.   if (picture_structure==FRAME_PICTURE)
  364.   {
  365.     motion_vector_count = (motion_type==MC_FIELD && stwclass<2) ? 2 : 1;
  366.     mv_format = (motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD;
  367.   }
  368.   else
  369.   {
  370.     motion_vector_count = (motion_type==MC_16X8) ? 2 : 1;
  371.     mv_format = MV_FIELD;
  372.   }
  373.  
  374.   dmv = (motion_type==MC_DMV); /* dual prime */
  375.  
  376.   /* field mv predictions in frame pictures have to be scaled
  377.    * ISO/IEC 13818-2 section 7.6.3.1 Decoding the motion vectors
  378.    * IMPLEMENTATION: mvscale is derived for later use in motion_vectors()
  379.    * it displaces the stage:
  380.    *
  381.    *    if((mv_format=="field")&&(t==1)&&(picture_structure=="Frame picture"))
  382.    *      prediction = PMV[r][s][t] DIV 2;
  383.    */
  384.  
  385.   mvscale = ((mv_format==MV_FIELD) && (picture_structure==FRAME_PICTURE));
  386.  
  387.   /* get dct_type (frame DCT / field DCT) */
  388.   dct_type = (picture_structure==FRAME_PICTURE)
  389.              && (!frame_pred_frame_dct)
  390.              && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA))
  391.              ? Get_Bits(1)
  392.              : 0;
  393.  
  394. #ifdef TRACE
  395.   if (Trace_Flag  && (picture_structure==FRAME_PICTURE)
  396.              && (!frame_pred_frame_dct)
  397.              && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA)))
  398.     printf("dct_type (%d): %s\n",dct_type,dct_type?"Field":"Frame");
  399. #endif /* TRACE */
  400.  
  401.   /* return values */
  402.   *pmacroblock_type = macroblock_type;
  403.   *pstwtype = stwtype;
  404.   *pstwclass = stwclass;
  405.   *pmotion_type = motion_type;
  406.   *pmotion_vector_count = motion_vector_count;
  407.   *pmv_format = mv_format;
  408.   *pdmv = dmv;
  409.   *pmvscale = mvscale;
  410.   *pdct_type = dct_type;
  411. }
  412.  
  413.  
  414. /* move/add 8x8-Block from block[comp] to backward_reference_frame */
  415. /* copy reconstructed 8x8 block from block[comp] to current_frame[]
  416.  * ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data
  417.  * This stage also embodies some of the operations implied by:
  418.  *   - ISO/IEC 13818-2 section 7.6.7: Combining predictions
  419.  *   - ISO/IEC 13818-2 section 6.1.3: Macroblock
  420. */
  421. static void Add_Block(comp,bx,by,dct_type,addflag)
  422. int comp,bx,by,dct_type,addflag;
  423. {
  424.   int cc,i, j, iincr;
  425.   unsigned char *rfp;
  426.   short *bp;
  427.  
  428.   
  429.   /* derive color component index */
  430.   /* equivalent to ISO/IEC 13818-2 Table 7-1 */
  431.   cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */
  432.  
  433.   if (cc==0)
  434.   {
  435.     /* luminance */
  436.  
  437.     if (picture_structure==FRAME_PICTURE)
  438.       if (dct_type)
  439.       {
  440.         /* field DCT coding */
  441.         rfp = current_frame[0]
  442.               + Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);
  443.         iincr = (Coded_Picture_Width<<1) - 8;
  444.       }
  445.       else
  446.       {
  447.         /* frame DCT coding */
  448.         rfp = current_frame[0]
  449.               + Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  450.         iincr = Coded_Picture_Width - 8;
  451.       }
  452.     else
  453.     {
  454.       /* field picture */
  455.       rfp = current_frame[0]
  456.             + (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);
  457.       iincr = (Coded_Picture_Width<<1) - 8;
  458.     }
  459.   }
  460.   else
  461.   {
  462.     /* chrominance */
  463.  
  464.     /* scale coordinates */
  465.     if (chroma_format!=CHROMA444)
  466.       bx >>= 1;
  467.     if (chroma_format==CHROMA420)
  468.       by >>= 1;
  469.     if (picture_structure==FRAME_PICTURE)
  470.     {
  471.       if (dct_type && (chroma_format!=CHROMA420))
  472.       {
  473.         /* field DCT coding */
  474.         rfp = current_frame[cc]
  475.               + Chroma_Width*(by+((comp&2)>>1)) + bx + (comp&8);
  476.         iincr = (Chroma_Width<<1) - 8;
  477.       }
  478.       else
  479.       {
  480.         /* frame DCT coding */
  481.         rfp = current_frame[cc]
  482.               + Chroma_Width*(by+((comp&2)<<2)) + bx + (comp&8);
  483.         iincr = Chroma_Width - 8;
  484.       }
  485.     }
  486.     else
  487.     {
  488.       /* field picture */
  489.       rfp = current_frame[cc]
  490.             + (Chroma_Width<<1)*(by+((comp&2)<<2)) + bx + (comp&8);
  491.       iincr = (Chroma_Width<<1) - 8;
  492.     }
  493.   }
  494.  
  495.   bp = ld->block[comp];
  496.  
  497.   if (addflag)
  498.   {
  499.     for (i=0; i<8; i++)
  500.     {
  501.       for (j=0; j<8; j++)
  502.       {
  503.         *rfp = Clip[*bp++ + *rfp];
  504.         rfp++;
  505.       }
  506.  
  507.       rfp+= iincr;
  508.     }
  509.   }
  510.   else
  511.   {
  512.     for (i=0; i<8; i++)
  513.     {
  514.       for (j=0; j<8; j++)
  515.         *rfp++ = Clip[*bp++ + 128];
  516.  
  517.       rfp+= iincr;
  518.     }
  519.   }
  520. }
  521.  
  522.  
  523. /* ISO/IEC 13818-2 section 7.8 */
  524. static void Decode_SNR_Macroblock(SNRMBA, SNRMBAinc, MBA, MBAmax, dct_type)
  525.   int *SNRMBA, *SNRMBAinc;
  526.   int MBA, MBAmax;
  527.   int *dct_type;
  528. {
  529.   int SNRmacroblock_type, SNRcoded_block_pattern, SNRdct_type, dummy; 
  530.   int slice_vert_pos_ext, quantizer_scale_code, comp, code;
  531.  
  532.   ld = &enhan;
  533.  
  534.   if (*SNRMBAinc==0)
  535.   {
  536.     if (!Show_Bits(23)) /* next_start_code */
  537.     {
  538.       next_start_code();
  539.       code = Show_Bits(32);
  540.  
  541.       if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
  542.       {
  543.         /* only slice headers are allowed in picture_data */
  544.         if (!Quiet_Flag)
  545.           printf("SNR: Premature end of picture\n");
  546.         return;
  547.       }
  548.  
  549.       Flush_Buffer32();
  550.  
  551.       /* decode slice header (may change quantizer_scale) */
  552.       slice_vert_pos_ext = slice_header();
  553.  
  554.       /* decode macroblock address increment */
  555.       *SNRMBAinc = Get_macroblock_address_increment();
  556.  
  557.       /* set current location */
  558.       *SNRMBA =
  559.         ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *SNRMBAinc - 1;
  560.  
  561.       *SNRMBAinc = 1; /* first macroblock in slice: not skipped */
  562.     }
  563.     else /* not next_start_code */
  564.     {
  565.       if (*SNRMBA>=MBAmax)
  566.       {
  567.         if (!Quiet_Flag)
  568.           printf("Too many macroblocks in picture\n");
  569.         return;
  570.       }
  571.  
  572.       /* decode macroblock address increment */
  573.       *SNRMBAinc = Get_macroblock_address_increment();
  574.     }
  575.   }
  576.  
  577.   if (*SNRMBA!=MBA)
  578.   {
  579.     /* streams out of sync */
  580.     if (!Quiet_Flag)
  581.       printf("Cant't synchronize streams\n");
  582.     return;
  583.   }
  584.  
  585.   if (*SNRMBAinc==1) /* not skipped */
  586.   {
  587.     macroblock_modes(&SNRmacroblock_type, &dummy, &dummy,
  588.       &dummy, &dummy, &dummy, &dummy, &dummy,
  589.       &SNRdct_type);
  590.  
  591.     if (SNRmacroblock_type & MACROBLOCK_PATTERN)
  592.       *dct_type = SNRdct_type;
  593.  
  594.     if (SNRmacroblock_type & MACROBLOCK_QUANT)
  595.     {
  596.       quantizer_scale_code = Get_Bits(5);
  597.       ld->quantizer_scale =
  598.         ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;
  599.     }
  600.  
  601.     /* macroblock_pattern */
  602.     if (SNRmacroblock_type & MACROBLOCK_PATTERN)
  603.     {
  604.       SNRcoded_block_pattern = Get_coded_block_pattern();
  605.  
  606.       if (chroma_format==CHROMA422)
  607.         SNRcoded_block_pattern = (SNRcoded_block_pattern<<2) | Get_Bits(2); /* coded_block_pattern_1 */
  608.       else if (chroma_format==CHROMA444)
  609.         SNRcoded_block_pattern = (SNRcoded_block_pattern<<6) | Get_Bits(6); /* coded_block_pattern_2 */
  610.     }
  611.     else
  612.       SNRcoded_block_pattern = 0;
  613.  
  614.     /* decode blocks */
  615.     for (comp=0; comp<block_count; comp++)
  616.     {
  617.       Clear_Block(comp);
  618.  
  619.       if (SNRcoded_block_pattern & (1<<(block_count-1-comp)))
  620.         Decode_MPEG2_Non_Intra_Block(comp);
  621.     }
  622.   }
  623.   else /* SNRMBAinc!=1: skipped macroblock */
  624.   {
  625.     for (comp=0; comp<block_count; comp++)
  626.       Clear_Block(comp);
  627.   }
  628.  
  629.   ld = &base;
  630. }
  631.  
  632.  
  633.  
  634. /* IMPLEMENTATION: set scratch pad macroblock to zero */
  635. static void Clear_Block(comp)
  636. int comp;
  637. {
  638.   short *Block_Ptr;
  639.   int i;
  640.  
  641.   Block_Ptr = ld->block[comp];
  642.  
  643.   for (i=0; i<64; i++)
  644.     *Block_Ptr++ = 0;
  645. }
  646.  
  647.  
  648. /* SCALABILITY: add SNR enhancement layer block data to base layer */
  649. /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from the two layes */
  650. static void Sum_Block(comp)
  651. int comp;
  652. {
  653.   short *Block_Ptr1, *Block_Ptr2;
  654.   int i;
  655.  
  656.   Block_Ptr1 = base.block[comp];
  657.   Block_Ptr2 = enhan.block[comp];
  658.  
  659.   for (i=0; i<64; i++)
  660.     *Block_Ptr1++ += *Block_Ptr2++;
  661. }
  662.  
  663.  
  664. /* limit coefficients to -2048..2047 */
  665. /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
  666. static void Saturate(Block_Ptr)
  667. short *Block_Ptr;
  668. {
  669.   int i, sum, val;
  670.  
  671.   sum = 0;
  672.  
  673.   /* ISO/IEC 13818-2 section 7.4.3: Saturation */
  674.   for (i=0; i<64; i++)
  675.   {
  676.     val = Block_Ptr[i];
  677.  
  678.     if (val>2047)
  679.       val = 2047;
  680.     else if (val<-2048)
  681.       val = -2048;
  682.  
  683.     Block_Ptr[i] = val;
  684.     sum+= val;
  685.   }
  686.  
  687.   /* ISO/IEC 13818-2 section 7.4.4: Mismatch control */
  688.   if ((sum&1)==0)
  689.     Block_Ptr[63]^= 1;
  690.  
  691. }
  692.  
  693.  
  694. /* reuse old picture buffers as soon as they are no longer needed 
  695.    based on life-time axioms of MPEG */
  696. static void Update_Picture_Buffers()
  697. {                           
  698.   int cc;              /* color component index */
  699.   unsigned char *tmp;  /* temporary swap pointer */
  700.  
  701.   for (cc=0; cc<3; cc++)
  702.   {
  703.     /* B pictures do not need to be save for future reference */
  704.     if (picture_coding_type==B_TYPE)
  705.     {
  706.       current_frame[cc] = auxframe[cc];
  707.     }
  708.     else
  709.     {
  710.       /* only update at the beginning of the coded frame */
  711.       if (!Second_Field)
  712.       {
  713.         tmp = forward_reference_frame[cc];
  714.  
  715.         /* the previously decoded reference frame is stored
  716.            coincident with the location where the backward 
  717.            reference frame is stored (backwards prediction is not
  718.            needed in P pictures) */
  719.         forward_reference_frame[cc] = backward_reference_frame[cc];
  720.         
  721.         /* update pointer for potential future B pictures */
  722.         backward_reference_frame[cc] = tmp;
  723.       }
  724.  
  725.       /* can erase over old backward reference frame since it is not used
  726.          in a P picture, and since any subsequent B pictures will use the 
  727.          previously decoded I or P frame as the backward_reference_frame */
  728.       current_frame[cc] = backward_reference_frame[cc];
  729.     }
  730.  
  731.     /* IMPLEMENTATION:
  732.        one-time folding of a line offset into the pointer which stores the
  733.        memory address of the current frame saves offsets and conditional 
  734.        branches throughout the remainder of the picture processing loop */
  735.     if (picture_structure==BOTTOM_FIELD)
  736.       current_frame[cc]+= (cc==0) ? Coded_Picture_Width : Chroma_Width;
  737.   }
  738. }
  739.  
  740.  
  741. /* store last frame */
  742.  
  743. void Output_Last_Frame_of_Sequence(Framenum)
  744. int Framenum;
  745. {
  746.   if (Second_Field)
  747.     printf("last frame incomplete, not stored\n");
  748.   else
  749.     Write_Frame(backward_reference_frame,Framenum-1);
  750. }
  751.  
  752.  
  753.  
  754. static void frame_reorder(Bitstream_Framenum, Sequence_Framenum)
  755. int Bitstream_Framenum, Sequence_Framenum;
  756. {
  757.   /* tracking variables to insure proper output in spatial scalability */
  758.   static int Oldref_progressive_frame, Newref_progressive_frame;
  759.  
  760.   if (Sequence_Framenum!=0)
  761.   {
  762.     if (picture_structure==FRAME_PICTURE || Second_Field)
  763.     {
  764.       if (picture_coding_type==B_TYPE)
  765.         Write_Frame(auxframe,Bitstream_Framenum-1);
  766.       else
  767.       {
  768.         Newref_progressive_frame = progressive_frame;
  769.         progressive_frame = Oldref_progressive_frame;
  770.  
  771.         Write_Frame(forward_reference_frame,Bitstream_Framenum-1);
  772.  
  773.         Oldref_progressive_frame = progressive_frame = Newref_progressive_frame;
  774.       }
  775.     }
  776. #ifdef DISPLAY
  777.     else if (Output_Type==T_X11)
  778.     {
  779.       if(!Display_Progressive_Flag)
  780.         Display_Second_Field();
  781.     }
  782. #endif
  783.   }
  784.   else
  785.     Oldref_progressive_frame = progressive_frame;
  786.  
  787. }
  788.  
  789.  
  790. /* ISO/IEC 13818-2 section 7.6 */
  791. static void motion_compensation(MBA, macroblock_type, motion_type, PMV, 
  792.   motion_vertical_field_select, dmvector, stwtype, dct_type)
  793. int MBA;
  794. int macroblock_type;
  795. int motion_type;
  796. int PMV[2][2][2];
  797. int motion_vertical_field_select[2][2];
  798. int dmvector[2];
  799. int stwtype;
  800. int dct_type;
  801. {
  802.   int bx, by;
  803.   int comp;
  804.     short t,*b;
  805.     int i,j;
  806.   /* derive current macroblock position within picture */
  807.   /* ISO/IEC 13818-2 section 6.3.1.6 and 6.3.1.7 */
  808.   bx = 16*(MBA%mb_width);
  809.   by = 16*(MBA/mb_width);
  810.  
  811.   /* motion compensation */
  812.   if (!(macroblock_type & MACROBLOCK_INTRA))
  813.     form_predictions(bx,by,macroblock_type,motion_type,PMV,
  814.       motion_vertical_field_select,dmvector,stwtype);
  815.   
  816.   /* SCALABILITY: Data Partitioning */
  817.   if (base.scalable_mode==SC_DP)
  818.     ld = &base;
  819.  
  820.   /* copy or add block data into picture */
  821.   for (comp=0; comp<block_count; comp++)
  822.   {
  823.     /* SCALABILITY: SNR */
  824.     /* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from 
  825.        the two a layers */
  826.     if (Two_Streams && enhan.scalable_mode==SC_SNR)
  827.       Sum_Block(comp); /* add SNR enhancement layer data to base layer */
  828.  
  829.     /* MPEG-2 saturation and mismatch control */
  830.     /* base layer could be MPEG-1 stream, enhancement MPEG-2 SNR */
  831.     /* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */
  832.     if ((Two_Streams && enhan.scalable_mode==SC_SNR) || ld->MPEG2_Flag)
  833.       Saturate(ld->block[comp]);
  834.  
  835.     
  836.  
  837.     /* ISO/IEC 13818-2 section Annex A: inverse DCT */
  838.     if (idctType==3)
  839.       Reference_IDCT(ld->block[comp]);
  840.     else if(idctType==2)
  841.       Fast_IDCT(ld->block[comp]);
  842.     else if(idctType==1){
  843.         ld->block[comp][0]+=1024;
  844.       MMX_IDCT(ld->block[comp]);
  845.       b=ld->block[comp];
  846.       for(j=0; j<8; j++)
  847.           for(i=j+1; i<8; i++){
  848.               t=b[i*8+j];
  849.               b[i*8+j]=b[j*8+i];
  850.               b[j*8+i]=t;
  851.           }
  852.       
  853.     }
  854.     
  855.     /* ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data */
  856.     Add_Block(comp,bx,by,dct_type,(macroblock_type & MACROBLOCK_INTRA)==0);
  857.   }
  858.  
  859. }
  860.  
  861.  
  862.  
  863. /* ISO/IEC 13818-2 section 7.6.6 */
  864. static void skipped_macroblock(dc_dct_pred, PMV, motion_type, 
  865.   motion_vertical_field_select, stwtype, macroblock_type)
  866. int dc_dct_pred[3];
  867. int PMV[2][2][2];
  868. int *motion_type;
  869. int motion_vertical_field_select[2][2];
  870. int *stwtype;
  871. int *macroblock_type;
  872. {
  873.   int comp;
  874.   
  875.   /* SCALABILITY: Data Paritioning */
  876.   if (base.scalable_mode==SC_DP)
  877.     ld = &base;
  878.  
  879.   for (comp=0; comp<block_count; comp++)
  880.     Clear_Block(comp);
  881.  
  882.   /* reset intra_dc predictors */
  883.   /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  884.   dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
  885.  
  886.   /* reset motion vector predictors */
  887.   /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  888.   if (picture_coding_type==P_TYPE)
  889.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  890.  
  891.   /* derive motion_type */
  892.   if (picture_structure==FRAME_PICTURE)
  893.     *motion_type = MC_FRAME;
  894.   else
  895.   {
  896.     *motion_type = MC_FIELD;
  897.  
  898.     /* predict from field of same parity */
  899.     /* ISO/IEC 13818-2 section 7.6.6.1 and 7.6.6.3: P field picture and B field
  900.        picture */
  901.     motion_vertical_field_select[0][0]=motion_vertical_field_select[0][1] = 
  902.       (picture_structure==BOTTOM_FIELD);
  903.   }
  904.  
  905.   /* skipped I are spatial-only predicted, */
  906.   /* skipped P and B are temporal-only predicted */
  907.   /* ISO/IEC 13818-2 section 7.7.6: Skipped macroblocks */
  908.   *stwtype = (picture_coding_type==I_TYPE) ? 8 : 0;
  909.  
  910.  /* IMPLEMENTATION: clear MACROBLOCK_INTRA */
  911.   *macroblock_type&= ~MACROBLOCK_INTRA;
  912.  
  913. }
  914.  
  915.  
  916. /* return==-1 means go to next picture */
  917. /* the expression "start of slice" is used throughout the normative
  918.    body of the MPEG specification */
  919. static int start_of_slice(MBAmax, MBA, MBAinc, 
  920.   dc_dct_pred, PMV)
  921. int MBAmax;
  922. int *MBA;
  923. int *MBAinc;
  924. int dc_dct_pred[3];
  925. int PMV[2][2][2];
  926. {
  927.   unsigned int code;
  928.   int slice_vert_pos_ext;
  929.  
  930.   ld = &base;
  931.  
  932.   Fault_Flag = 0;
  933.  
  934.   next_start_code();
  935.   code = Show_Bits(32);
  936.  
  937.   if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
  938.   {
  939.     /* only slice headers are allowed in picture_data */
  940.     if (!Quiet_Flag)
  941.       printf("start_of_slice(): Premature end of picture\n");
  942.  
  943.     return(-1);  /* trigger: go to next picture */
  944.   }
  945.  
  946.   Flush_Buffer32(); 
  947.  
  948.   /* decode slice header (may change quantizer_scale) */
  949.   slice_vert_pos_ext = slice_header();
  950.  
  951.  
  952.   /* SCALABILITY: Data Partitioning */
  953.   if (base.scalable_mode==SC_DP)
  954.   {
  955.     ld = &enhan;
  956.     next_start_code();
  957.     code = Show_Bits(32);
  958.  
  959.     if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)
  960.     {
  961.       /* only slice headers are allowed in picture_data */
  962.       if (!Quiet_Flag)
  963.         printf("DP: Premature end of picture\n");
  964.       return(-1);    /* trigger: go to next picture */
  965.     }
  966.  
  967.     Flush_Buffer32();
  968.  
  969.     /* decode slice header (may change quantizer_scale) */
  970.     slice_vert_pos_ext = slice_header();
  971.  
  972.     if (base.priority_breakpoint!=1)
  973.       ld = &base;
  974.   }
  975.  
  976.   /* decode macroblock address increment */
  977.   *MBAinc = Get_macroblock_address_increment();
  978.  
  979.   if (Fault_Flag) 
  980.   {
  981.     printf("start_of_slice(): MBAinc unsuccessful\n");
  982.     return(0);   /* trigger: go to next slice */
  983.   }
  984.  
  985.   /* set current location */
  986.   /* NOTE: the arithmetic used to derive macroblock_address below is
  987.    *       equivalent to ISO/IEC 13818-2 section 6.3.17: Macroblock
  988.    */
  989.   *MBA = ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *MBAinc - 1;
  990.   *MBAinc = 1; /* first macroblock in slice: not skipped */
  991.  
  992.   /* reset all DC coefficient and motion vector predictors */
  993.   /* reset all DC coefficient and motion vector predictors */
  994.   /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  995.   dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
  996.   
  997.   /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  998.   PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  999.   PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  1000.  
  1001.   /* successfull: trigger decode macroblocks in slice */
  1002.   return(1);
  1003. }
  1004.  
  1005.  
  1006. /* ISO/IEC 13818-2 sections 7.2 through 7.5 */
  1007. static int decode_macroblock(macroblock_type, stwtype, stwclass,
  1008.   motion_type, dct_type, PMV, dc_dct_pred, 
  1009.   motion_vertical_field_select, dmvector)
  1010. int *macroblock_type; 
  1011. int *stwtype;
  1012. int *stwclass;
  1013. int *motion_type; 
  1014. int *dct_type;
  1015. int PMV[2][2][2]; 
  1016. int dc_dct_pred[3]; 
  1017. int motion_vertical_field_select[2][2];
  1018. int dmvector[2];
  1019. {
  1020.   /* locals */
  1021.   int quantizer_scale_code; 
  1022.   int comp;
  1023.  
  1024.   int motion_vector_count; 
  1025.   int mv_format; 
  1026.   int dmv; 
  1027.   int mvscale;
  1028.   int coded_block_pattern;
  1029.  
  1030.   /* SCALABILITY: Data Patitioning */
  1031.   if (base.scalable_mode==SC_DP)
  1032.   {
  1033.     if (base.priority_breakpoint<=2)
  1034.       ld = &enhan;
  1035.     else
  1036.       ld = &base;
  1037.   }
  1038.  
  1039.   /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
  1040.   macroblock_modes(macroblock_type, stwtype, stwclass,
  1041.     motion_type, &motion_vector_count, &mv_format, &dmv, &mvscale,
  1042.     dct_type);
  1043.  
  1044.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1045.  
  1046.   if (*macroblock_type & MACROBLOCK_QUANT)
  1047.   {
  1048.     quantizer_scale_code = Get_Bits(5);
  1049.  
  1050. #ifdef TRACE
  1051.     if (Trace_Flag)
  1052.     {
  1053.       printf("quantiser_scale_code (");
  1054.       Print_Bits(quantizer_scale_code,5,5);
  1055.       printf("): %d\n",quantizer_scale_code);
  1056.     }
  1057. #endif /* TRACE */
  1058.  
  1059.     /* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
  1060.     if (ld->MPEG2_Flag)
  1061.       ld->quantizer_scale =
  1062.       ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] 
  1063.        : (quantizer_scale_code << 1);
  1064.     else
  1065.       ld->quantizer_scale = quantizer_scale_code;
  1066.  
  1067.     /* SCALABILITY: Data Partitioning */
  1068.     if (base.scalable_mode==SC_DP)
  1069.       /* make sure base.quantizer_scale is valid */
  1070.       base.quantizer_scale = ld->quantizer_scale;
  1071.   }
  1072.  
  1073.   /* motion vectors */
  1074.  
  1075.  
  1076.   /* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
  1077.  
  1078.   /* decode forward motion vectors */
  1079.   if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD) 
  1080.     || ((*macroblock_type & MACROBLOCK_INTRA) 
  1081.     && concealment_motion_vectors))
  1082.   {
  1083.     if (ld->MPEG2_Flag)
  1084.       motion_vectors(PMV,dmvector,motion_vertical_field_select,
  1085.         0,motion_vector_count,mv_format,f_code[0][0]-1,f_code[0][1]-1,
  1086.         dmv,mvscale);
  1087.     else
  1088.       motion_vector(PMV[0][0],dmvector,
  1089.       forward_f_code-1,forward_f_code-1,0,0,full_pel_forward_vector);
  1090.   }
  1091.  
  1092.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1093.  
  1094.   /* decode backward motion vectors */
  1095.   if (*macroblock_type & MACROBLOCK_MOTION_BACKWARD)
  1096.   {
  1097.     if (ld->MPEG2_Flag)
  1098.       motion_vectors(PMV,dmvector,motion_vertical_field_select,
  1099.         1,motion_vector_count,mv_format,f_code[1][0]-1,f_code[1][1]-1,0,
  1100.         mvscale);
  1101.     else
  1102.       motion_vector(PMV[0][1],dmvector,
  1103.         backward_f_code-1,backward_f_code-1,0,0,full_pel_backward_vector);
  1104.   }
  1105.  
  1106.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1107.  
  1108.   if ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
  1109.     Flush_Buffer(1); /* remove marker_bit */
  1110.  
  1111.   if (base.scalable_mode==SC_DP && base.priority_breakpoint==3)
  1112.     ld = &enhan;
  1113.  
  1114.   /* macroblock_pattern */
  1115.   /* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
  1116.   if (*macroblock_type & MACROBLOCK_PATTERN)
  1117.   {
  1118.     coded_block_pattern = Get_coded_block_pattern();
  1119.  
  1120.     if (chroma_format==CHROMA422)
  1121.     {
  1122.       /* coded_block_pattern_1 */
  1123.       coded_block_pattern = (coded_block_pattern<<2) | Get_Bits(2); 
  1124.  
  1125. #ifdef TRACE
  1126.        if (Trace_Flag)
  1127.        {
  1128.          printf("coded_block_pattern_1: ");
  1129.          Print_Bits(coded_block_pattern,2,2);
  1130.          printf(" (%d)\n",coded_block_pattern&3);
  1131.        }
  1132. #endif /* TRACE */
  1133.      }
  1134.      else if (chroma_format==CHROMA444)
  1135.      {
  1136.       /* coded_block_pattern_2 */
  1137.       coded_block_pattern = (coded_block_pattern<<6) | Get_Bits(6); 
  1138.  
  1139. #ifdef TRACE
  1140.       if (Trace_Flag)
  1141.       {
  1142.         printf("coded_block_pattern_2: ");
  1143.         Print_Bits(coded_block_pattern,6,6);
  1144.         printf(" (%d)\n",coded_block_pattern&63);
  1145.       }
  1146. #endif /* TRACE */
  1147.     }
  1148.   }
  1149.   else
  1150.     coded_block_pattern = (*macroblock_type & MACROBLOCK_INTRA) ? 
  1151.       (1<<block_count)-1 : 0;
  1152.  
  1153.   if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1154.  
  1155.   /* decode blocks */
  1156.   for (comp=0; comp<block_count; comp++)
  1157.   {
  1158.     /* SCALABILITY: Data Partitioning */
  1159.     if (base.scalable_mode==SC_DP)
  1160.     ld = &base;
  1161.  
  1162.     Clear_Block(comp);
  1163.  
  1164.     if (coded_block_pattern & (1<<(block_count-1-comp)))
  1165.     {
  1166.       if (*macroblock_type & MACROBLOCK_INTRA)
  1167.       {
  1168.         if (ld->MPEG2_Flag)
  1169.           Decode_MPEG2_Intra_Block(comp,dc_dct_pred);
  1170.         else
  1171.           Decode_MPEG1_Intra_Block(comp,dc_dct_pred);
  1172.       }
  1173.       else
  1174.       {
  1175.         if (ld->MPEG2_Flag)
  1176.           Decode_MPEG2_Non_Intra_Block(comp);
  1177.         else
  1178.           Decode_MPEG1_Non_Intra_Block(comp);
  1179.       }
  1180.  
  1181.       if (Fault_Flag) return(0);  /* trigger: go to next slice */
  1182.     }
  1183.   }
  1184.  
  1185.   if(picture_coding_type==D_TYPE)
  1186.   {
  1187.     /* remove end_of_macroblock (always 1, prevents startcode emulation) */
  1188.     /* ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
  1189.     marker_bit("D picture end_of_macroblock bit");
  1190.   }
  1191.  
  1192.   /* reset intra_dc predictors */
  1193.   /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  1194.   if (!(*macroblock_type & MACROBLOCK_INTRA))
  1195.     dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
  1196.  
  1197.   /* reset motion vector predictors */
  1198.   if ((*macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
  1199.   {
  1200.     /* intra mb without concealment motion vectors */
  1201.     /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  1202.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1203.     PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  1204.   }
  1205.  
  1206.   /* special "No_MC" macroblock_type case */
  1207.   /* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
  1208.   if ((picture_coding_type==P_TYPE) 
  1209.     && !(*macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
  1210.   {
  1211.     /* non-intra mb without forward mv in a P picture */
  1212.     /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
  1213.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1214.  
  1215.     /* derive motion_type */
  1216.     /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
  1217.     if (picture_structure==FRAME_PICTURE)
  1218.       *motion_type = MC_FRAME;
  1219.     else
  1220.     {
  1221.       *motion_type = MC_FIELD;
  1222.       /* predict from field of same parity */
  1223.       motion_vertical_field_select[0][0] = (picture_structure==BOTTOM_FIELD);
  1224.     }
  1225.   }
  1226.  
  1227.   if (*stwclass==4)
  1228.   {
  1229.     /* purely spatially predicted macroblock */
  1230.     /* ISO/IEC 13818-2 section 7.7.5.1: Resetting motion vector predictions */
  1231.     PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
  1232.     PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  1233.   }
  1234.  
  1235.   /* successfully decoded macroblock */
  1236.   return(1);
  1237.  
  1238. } /* decode_macroblock */
  1239.  
  1240.  
  1241.